/*
 * Niiuzu
 * Copyright (C) 2011  Thierry Nowak
 * 
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.niiuzu.client.screen.feed.viewer;

import java.util.Date;
import java.util.Map;

import com.google.gwt.resources.client.ImageResource;
import com.mvp4g.client.annotation.Presenter;
import com.mvp4g.client.presenter.LazyPresenter;
import com.niiuzu.client.eventbus.FeedEventBus;
import com.niiuzu.client.util.SessionClient;
import com.niiuzu.client.util.images.BundleFactory;
import com.niiuzu.client.util.images.feed.FeedImageBundle;
import com.niiuzu.shared.model.FeedChannel;
import com.niiuzu.shared.model.FeedChannelGroup;
import com.niiuzu.shared.model.FeedEntry;
import com.niiuzu.shared.model.FeedStyle;

/**
 * Presenter dédié à la consultation des données provenant des flux RSS.
 */
@Presenter(view = FeedViewerViewImpl.class)
public class FeedViewerPresenter extends
		LazyPresenter<FeedViewerView, FeedEventBus> {

	private FeedImageBundle feedImageBundle = BundleFactory
			.getFeedImageBundle();

	/**
	 * La consultation des données provenant des flux RSS est en cours de
	 * chargement.
	 * 
	 * @see FeedEventBus#feedViewerLoadingStarted()
	 */
	public void onFeedViewerLoadingStarted() {
		eventBus.feedViewerLoaded(view);
	}

	/**
	 * Réception de nouvelles données provenant de flux RSS.
	 * 
	 * @see FeedEventBus#feedEntriesReceived(FeedEntry[])
	 */
	public void onFeedEntriesReceived(FeedEntry[] feedEntries) {

		SessionClient sessionClient = SessionClient.getInstance();

		Map<String, FeedChannel> channelMap = sessionClient.getChannelMap();
		Map<String, FeedChannelGroup> channelGroupMap = sessionClient
				.getChannelGroupMap();

		view.clearAllFeedData();
		for (FeedEntry entry : feedEntries) {

			String link = entry.getLink();
			Date date = entry.getDate();
			String summary = entry.getSummary();

			ImageResource icon = null;

			String channelGroupName = "";
			if (channelMap.containsKey(entry.getChannelUid())) {
				FeedChannel channel = channelMap.get(entry.getChannelUid());
				String channelGroupUid = channel.getChannelGroupUid();
				if (channelGroupMap.containsKey(channelGroupUid)) {
					FeedChannelGroup channelGroup = channelGroupMap
							.get(channelGroupUid);

					icon = getImageResourceFromStyle(channelGroup.getStyle());

					channelGroupName = channelGroup.getName();
				}
			}

			view.addFeedEntry(link, icon, channelGroupName, date, summary);
		}
	}

	/**
	 * Récupère une {@link ImageResource} en fonction du {@link FeedStyle}.
	 * Cette ressource provient du bundle {@link FeedImageBundle}.
	 */
	ImageResource getImageResourceFromStyle(FeedStyle style) {

		if (style != null) {

			switch (style) {
			case BLACK:
				return feedImageBundle.black();
			case BLUE:
				return feedImageBundle.blue();
			case GREEN:
				return feedImageBundle.green();
			case ORANGE:
				return feedImageBundle.orange();
			case PURPLE:
				return feedImageBundle.purple();
			case RED:
				return feedImageBundle.red();
			case YELLOW:
				return feedImageBundle.yellow();
			}
		}

		return feedImageBundle.defaultImage();
	}

	/* ********************************************************************** */
	/* ********************************************************************** */
	/* ********************************************************************** */

	/**
	 * JUnit - Définit un mock {@link FeedImageBundle}.
	 */
	void setMockFeedImageBundle(FeedImageBundle mockedFeedImageBundle) {
		this.feedImageBundle = mockedFeedImageBundle;
	}
}
